"""
  Custom form widget classes specific to skoolmaster site.
"""
import copy
from django import forms
from django.utils.safestring import mark_safe
from skoolmaster import settings

class CustomDateWidget(forms.TextInput):
    class Media:
        js = ("js/calendar.js",
              "js/DateTimeShortcuts.js")

    def __init__(self, attrs={}):
        super(CustomDateWidget, self).__init__(attrs={'class': 'vDateField', 'size': '10'})

class CustomTimeWidget(forms.TextInput):
    class Media:
        js = ("js/calendar.js",
              "js/DateTimeShortcuts.js")

    def __init__(self, attrs={}):
        super(CustomTimeWidget, self).__init__(attrs={'class': 'vTimeField', 'size': '8'})

class CustomSplitDateTime(forms.SplitDateTimeWidget):
    """
    A SplitDateTime Widget that has some admin-specific styling.
    """
    def __init__(self, attrs=None):
        widgets = [CustomDateWidget, CustomTimeWidget]
        # Note that we're calling MultiWidget, not SplitDateTimeWidget, because
        # we want to define widgets.
        forms.MultiWidget.__init__(self, widgets, attrs)

    def format_output(self, rendered_widgets):
        return mark_safe(u'<p class="datetime">%s %s<br />%s %s</p>' % \
            ('Datum:', rendered_widgets[0], 'Ura:', rendered_widgets[1]))
        


class HorizontalFilterWidget(forms.SelectMultiple):
  """
  Fancy JavaScript multiple selection field.
  """
  def __init__(self, verbose_name, title="", is_stacked=False, attrs=None, choices=()):
    self.verbose_name = verbose_name
    self.title = title
    self.is_stacked = is_stacked
    super(HorizontalFilterWidget, self).__init__(attrs, choices)
    
  def render(self, name, value, attrs=None, choices=()):
    verbose_name = self.verbose_name.replace('"', '\\"')
    output = [super(HorizontalFilterWidget, self).render(verbose_name, value, attrs, choices)]
    output.append(u'<script type="text/javascript">addEvent(window, "load", function(e) {')
    output.append(u'SelectFilter.init("id_%s", "%s", "%s", %s, "%s"); });</script>\n' % \
      (name, verbose_name, self.title, int(self.is_stacked), settings.MEDIA_URL))
    return mark_safe(u''.join(output))
